package com.zhuchaooasystem.presenter;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.UserEntity;
import com.zhuchaooasystem.entity.UserInfo;
import com.zhuchaooasystem.utils.EncryptUtils;
import com.zhuchaooasystem.widget.OkDialog;

import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import rx.Subscription;
import rx.functions.Action1;


/**
 * Created by zhangyajiang on 2016/12/22.
 */

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter {

    public MainPresenter(Activity activity, MainContract.View view) {
        super(activity, view);
    }

    /**
     * 检查当前是否有用户已登录
     *
     * @return
     */
    public boolean checkLoginState() {
        return UserInfo.getInstance().isNotEmpty();
    }

    /**
     * 获取当前数据库登录的用户信息
     *
     * @return
     */
    public UserInfo getUserInfo() {
        return UserInfo.getInstance();
    }

    @Override
    public void login() {
        UserInfo user = getUserInfo();
        String username = user.getUserName();
        String password = user.getPassword();
        //----
        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && password.length() >= 6) {
            //----
            final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
            map.put("LoginName", username);
            map.put("Passcode", EncryptUtils.encryptPassword(password));
            map.put("type", EncryptUtils.getuniqueId(mActivity));
            map.put("Alias", Build.MODEL);
            //----
            Subscription subscription = HttpFactory.getHttpApi().loginApi(map)
                    .compose(SchedulersCompat.applyIoSchedulers())
                    .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                        @Override
                        public void call(Object o) {
                            if (null != o) {
                                UserEntity userEntity = (UserEntity) o;
                                if (null != userEntity.getResult().getSuccessful()) {
                                    mView.setHasLogin(true);
                                } else if (null != userEntity.getResult().getError()) {
                                    if (userEntity.getResult().getError().equals("用户已经离职")) {
                                        JPushInterface.stopPush(mActivity);
                                        //----清空登录记录
                                        UserInfo.getInstance().clearLoginInfo();
                                        //----
                                        OkDialog dialogOk = new OkDialog(mActivity);
                                        dialogOk.show();
                                        dialogOk.setTextContent("您已经离职");
                                        dialogOk.setOnOkClickListener(new OkDialog.OnOkClickListener() {
                                            @Override
                                            public void onClick(OkDialog dialog) {
                                                dialog.dismiss();
                                            }
                                        });
                                    }
                                }

                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                        }
                    });
            addSubscrebe(subscription);
        }
    }

}
